package component;

import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import omq.screen.DefaultScreen;
import constant.OmqTesterClientConst;

public class OmqClientMenu {
	private static JMenuBar menuBar;
	private static OmqClientMenu clientMenu;
	
	public static OmqClientMenu getInstance(){
		if(clientMenu == null){
			clientMenu = new OmqClientMenu();
		}
		return clientMenu;
	}
	
	public OmqClientMenu(){
		menuBar = new JMenuBar();
	}
	
	public void addMenu(String name,int keyEvent){
		JMenu menu = new JMenu(name);
		menu.setMnemonic(keyEvent);
		menu.setName(name);
		menuBar.add(menu);
	}
	
	public void addActionsMenu(ActionListener listener){
		//Creating the Actions menu
		JMenuItem menuItem;
		JMenu menu = new JMenu(OmqTesterClientConst.ACTIONS);
		menu.setName(OmqTesterClientConst.ACTIONS);
		
		//Adding a submenu : Message 
		JMenu msgSubMenu = new JMenu(OmqTesterClientConst.ACTIONS_MESSAGE_SUBMENU);
		msgSubMenu.setName(OmqTesterClientConst.ACTIONS_MESSAGE_SUBMENU);
		//1
		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_SEND_MSG,DefaultScreen.createImageIcon("../../images/sendIco.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.ACTION_SEND_MSG);
		menuItem.addActionListener(listener);		
		msgSubMenu.add(menuItem);
		//2
		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_RECIEVE_MSG,DefaultScreen.createImageIcon("../../images/getMsg.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.ACTION_RECIEVE_MSG);
		menuItem.addActionListener(listener);		
		msgSubMenu.add(menuItem);
		//3
		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_PEEK_MSG,DefaultScreen.createImageIcon("../../images/peekMsg.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.ACTION_PEEK_MSG);
		menuItem.addActionListener(listener);		
		msgSubMenu.add(menuItem);
		//4
//		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_DELETE_MSG,DefaultScreen.createImageIcon("../../images/deleteMsg.png",""));
//		menuItem.setActionCommand(OmqTesterClientConst.ACTION_DELETE_MSG);
//		menuItem.addActionListener(listener);		
//		msgSubMenu.add(menuItem);
		menu.add(msgSubMenu);

		//Adding a submenu : Connection 
		JMenu connSubMenu = new JMenu(OmqTesterClientConst.ACTIONS_CONNECTION_SUBMENU);
		connSubMenu.setName(OmqTesterClientConst.ACTIONS_CONNECTION_SUBMENU);
		menu.add(connSubMenu);
		//1
//		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_OPEN_CONNECTION,DefaultScreen.createImageIcon("../../images/openConn.png",""));
//		menuItem.setActionCommand(OmqTesterClientConst.ACTION_OPEN_CONNECTION);
//		menuItem.addActionListener(listener);		
//		connSubMenu.add(menuItem);
//		//2
//		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_CLOSE_CONNECTION,DefaultScreen.createImageIcon("../../images/closeConn.png",""));
//		menuItem.setActionCommand(OmqTesterClientConst.ACTION_CLOSE_CONNECTION);
//		menuItem.addActionListener(listener);		
//		connSubMenu.add(menuItem);
//		//Seperator
//		connSubMenu.addSeparator();
		//3
		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_CHECK_CONNECTION,DefaultScreen.createImageIcon("../../images/checkConn.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.ACTION_CHECK_CONNECTION);
		menuItem.addActionListener(listener);		
		connSubMenu.add(menuItem);
		//seperator
		

		//Adding a submenu : Queue 
		JMenu queueSubMenu = new JMenu(OmqTesterClientConst.ACTIONS_QUEUE_SUBMENU);
		queueSubMenu.setName(OmqTesterClientConst.ACTIONS_QUEUE_SUBMENU);
		menu.add(queueSubMenu);
		//1
		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_QUEUE_PROPERTIES,DefaultScreen.createImageIcon("../../images/peekMsg.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.ACTION_QUEUE_PROPERTIES);
		menuItem.addActionListener(listener);		
		queueSubMenu.add(menuItem);
		//2
		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_EMPTY_QUEUE,DefaultScreen.createImageIcon("../../images/getMsg.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.ACTION_EMPTY_QUEUE);
		menuItem.addActionListener(listener);		
		queueSubMenu.add(menuItem);

		
		
		//seperator
		menu.addSeparator();
		//Close omq client 
		menuItem = new JMenuItem(OmqTesterClientConst.ACTION_CLOSE_CLIENT,DefaultScreen.createImageIcon("../../images/exit.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.ACTION_CLOSE_CLIENT);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		menuBar.add(menu);
	}
	
	public void addPrefMenu(ActionListener listener){
		JMenuItem menuItem;
		JMenu menu = new JMenu(OmqTesterClientConst.PREFERENCES);
		menu.setName(OmqTesterClientConst.PREFERENCES);
		//1
		menuItem = new JMenuItem(OmqTesterClientConst.PREFERENCES_EDIT,DefaultScreen.createImageIcon("../../images/editPref.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.PREFERENCES_EDIT);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);		
		//2
//		menuItem = new JMenuItem(OmqTesterClientConst.PREFERENCES_CLEAR,DefaultScreen.createImageIcon("../../images/deletePref.png",""));
//		menuItem.setActionCommand(OmqTesterClientConst.PREFERENCES_CLEAR);
//		menuItem.addActionListener(listener);		
//		menu.add(menuItem);
		
		menuBar.add(menu);
	}
	
	public void addSessionMenu(ActionListener listener){
		JMenuItem menuItem;
		JMenu menu = new JMenu(OmqTesterClientConst.SESSION);
		menu.setName(OmqTesterClientConst.SESSION);
		//1
		menuItem = new JMenuItem(OmqTesterClientConst.SESSION_OPEN);
		menuItem.setActionCommand(OmqTesterClientConst.SESSION_OPEN);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		//2
		menuItem = new JMenuItem(OmqTesterClientConst.SESSION_CLOSE);
		menuItem.setActionCommand(OmqTesterClientConst.SESSION_CLOSE);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		//Seperator
		menu.addSeparator();
		//Adding transacional messages handling menu
		menu.add(addTransactionalMessagesHandlingMenu(listener));
		//Seperator
		menu.addSeparator();
		//3
		menuItem = new JMenuItem(OmqTesterClientConst.SESSION_COMMIT);
		menuItem.setActionCommand(OmqTesterClientConst.SESSION_COMMIT);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		//4
		menuItem = new JMenuItem(OmqTesterClientConst.SESSION_ROLLBACK);
		menuItem.setActionCommand(OmqTesterClientConst.SESSION_ROLLBACK);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		
		menuBar.add(menu);
	}
	
	private JMenu addTransactionalMessagesHandlingMenu(ActionListener listener){
		JMenuItem menuItem;
		JMenu menu = new JMenu(OmqTesterClientConst.ACTIONS);
		menu.setName(OmqTesterClientConst.ACTIONS);
		//1
		menuItem = new JMenuItem(OmqTesterClientConst.SESSION_SEND);
		menuItem.setActionCommand(OmqTesterClientConst.SESSION_SEND);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		//2
		menuItem = new JMenuItem(OmqTesterClientConst.SESSION_RECIEVE);
		menuItem.setActionCommand(OmqTesterClientConst.SESSION_RECIEVE);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		//3
		menuItem = new JMenuItem(OmqTesterClientConst.SESSION_DELETE);
		menuItem.setActionCommand(OmqTesterClientConst.SESSION_DELETE);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		
		return menu;
	}
	
	
	public void addHelpMenu(ActionListener listener){
		JMenuItem menuItem;
		JMenu menu = new JMenu(OmqTesterClientConst.HELP);
		menu.setName(OmqTesterClientConst.HELP);
		//1
		menuItem = new JMenuItem(OmqTesterClientConst.HELP_ABOUT,DefaultScreen.createImageIcon("../../images/help.png",""));
		menuItem.setActionCommand(OmqTesterClientConst.HELP_ABOUT);
		menuItem.addActionListener(listener);		
		menu.add(menuItem);
		
		menuBar.add(menu);
	}
	
	public JMenuBar getMenuBar(){
		return menuBar;
	}
	
	
	public void addMenu(String name,int keyEvent,Map menuItems,int menuSeperator,ActionListener listener){
		int index = 0;
		JMenu menu = new JMenu(name);
		menu.setMnemonic(keyEvent);
		menu.setName(name);
		menuBar.add(menu);
		//Adding the menu items to the menu
		Iterator valuesIterator = menuItems.entrySet().iterator();
		while(valuesIterator.hasNext()){
			if(index == menuSeperator){
				menu.addSeparator();
			}
			String str = ((Entry)valuesIterator.next()).toString();
			int seperator = str.indexOf("=");
			String key = str.substring(0,seperator);
			String value = str.substring(seperator + 1,str.length());
			addMenuItem(name,key,Integer.parseInt(value),listener);
			index++;
		}
	}
	

	
	public void addMenuItem(String menuName,String itemName,int keyEvent,ActionListener listener){
		int menu_count = menuBar.getMenuCount();
		for(int index = 0; index < menu_count; ++index ) {
			JMenu found = menuBar.getMenu(index);
			 if( found.getName().equals( menuName ) ){
				 JMenuItem menuItem = new JMenuItem(itemName,keyEvent);
				 menuItem.setActionCommand(itemName);
				 menuItem.addActionListener(listener);
				 found.add(menuItem);
				 break;
			 }
		}
	}
	
	
}
